@echo off
echo ========================================
echo 🔧 自动配置环境变量工具
echo ========================================
echo.

:: 设置管理员权限标志
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo ⚠️  需要管理员权限来配置系统环境变量
    echo 💡 请右键点击此文件，选择"以管理员身份运行"
    pause
    exit /b 1
)

echo ✅ 检测到管理员权限
echo.

:: 配置Java环境变量
echo [1/3] 配置 Java 环境变量...
if exist "D:\JAVA\bin\java.exe" (
    echo 📍 发现Java安装路径: D:\JAVA
    
    :: 设置JAVA_HOME
    setx JAVA_HOME "D:\JAVA" /M
    echo ✅ 设置 JAVA_HOME = D:\JAVA
    
    :: 添加到PATH
    for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH 2^>nul') do set "SYSTEM_PATH=%%b"
    echo %SYSTEM_PATH% | findstr /i "D:\JAVA\bin" >nul
    if %errorlevel% neq 0 (
        setx PATH "%SYSTEM_PATH%;D:\JAVA\bin" /M
        echo ✅ 添加 D:\JAVA\bin 到系统PATH
    ) else (
        echo ✅ Java已在PATH中
    )
) else (
    echo ❌ 未找到Java安装，请检查安装是否完成
)
echo.

:: 配置Python环境变量
echo [2/3] 配置 Python 环境变量...
set PYTHON_FOUND=0

:: 常见Python安装路径
for %%p in (
    "C:\Python313"
    "C:\Python312"
    "C:\Python311"
    "C:\Python310"
    "C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python313"
    "C:\Users\%USERNAME%\AppData\Local\Programs\Python\Python312"
    "C:\Program Files\Python313"
    "C:\Program Files\Python312"
) do (
    if exist "%%~p\python.exe" (
        echo 📍 发现Python安装路径: %%~p
        
        :: 添加Python到PATH
        for /f "tokens=2*" %%a in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH 2^>nul') do set "SYSTEM_PATH=%%b"
        echo %SYSTEM_PATH% | findstr /i "%%~p" >nul
        if %errorlevel% neq 0 (
            setx PATH "%SYSTEM_PATH%;%%~p;%%~p\Scripts" /M
            echo ✅ 添加 %%~p 到系统PATH
            echo ✅ 添加 %%~p\Scripts 到系统PATH
        ) else (
            echo ✅ Python已在PATH中
        )
        set PYTHON_FOUND=1
        goto :python_done
    )
)

:python_done
if %PYTHON_FOUND%==0 (
    echo ❌ 未找到Python安装，请检查安装是否完成
)
echo.

:: 配置Android SDK环境变量
echo [3/3] 配置 Android SDK 环境变量...
set ANDROID_FOUND=0

:: 常见Android SDK路径
for %%a in (
    "C:\Users\%USERNAME%\AppData\Local\Android\Sdk"
    "C:\Android\Sdk"
    "D:\Android\Sdk"
    "C:\Program Files\Android\Android Studio\sdk"
    "C:\Program Files (x86)\Android\android-sdk"
) do (
    if exist "%%~a\platform-tools\adb.exe" (
        echo 📍 发现Android SDK路径: %%~a
        
        :: 设置ANDROID_HOME
        setx ANDROID_HOME "%%~a" /M
        echo ✅ 设置 ANDROID_HOME = %%~a
        
        :: 添加到PATH
        for /f "tokens=2*" %%b in ('reg query "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v PATH 2^>nul') do set "SYSTEM_PATH=%%c"
        echo %SYSTEM_PATH% | findstr /i "%%~a\platform-tools" >nul
        if %errorlevel% neq 0 (
            setx PATH "%SYSTEM_PATH%;%%~a\platform-tools;%%~a\tools" /M
            echo ✅ 添加 %%~a\platform-tools 到系统PATH
            echo ✅ 添加 %%~a\tools 到系统PATH
        ) else (
            echo ✅ Android SDK已在PATH中
        )
        set ANDROID_FOUND=1
        goto :android_done
    )
)

:android_done
if %ANDROID_FOUND%==0 (
    echo ⚠️  未找到Android SDK，这通常在首次打开Android Studio时自动安装
    echo 💡 请先打开Android Studio，它会自动下载SDK
)
echo.

echo ========================================
echo 🎉 环境变量配置完成！
echo ========================================
echo.
echo 📋 配置摘要：
if exist "D:\JAVA\bin\java.exe" echo ✅ Java JDK: D:\JAVA
if %PYTHON_FOUND%==1 echo ✅ Python: 已配置
if %ANDROID_FOUND%==1 echo ✅ Android SDK: 已配置
echo.
echo ⚠️  重要提示：
echo 1. 请重启命令行窗口使环境变量生效
echo 2. 或者注销并重新登录Windows
echo 3. 然后运行 verify_setup.bat 验证配置
echo.
pause 